package labb1.del2;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Class to create Hands.
 *
 * @author Mikko Puustinen
 * @version 1.0, 2007-sep-14
 */
public class Hand {
    private ArrayList<Card> hand;

    /**
     * Creates an empty hand.
     */
    public Hand() {
        hand = new ArrayList<Card>();
    }

    /**
     * Add a <code>Card</code> to the <code>hand</code>.
     *
     * @param card the <code>Card</code> object to add to the
     *             <code>hand</code>.
     */
    public void addCard(Card card) {
        hand.add(card);
    }

    /**
     * Removes the last <code>Card</code> from the hand.
     *
     * @return the removed card.
     */
    public Card getCard() {
        int card = hand.size() - 1;
        if (card < 0)
            throw new NoSuchCardException("Hand is empty", card);
        return hand.remove(card);
    }

    /**
     * Removes the <code>Card</code>.
     *
     * @param card position of the card to remove.
     *
     * @return the removed card.
     */
    public Card getCard(int card) {
        if (card < 0 || card > hand.size() - 1)
            throw new NoSuchCardException(card);
        return hand.remove(card);
    }

    /**
     * Removes the <code>Card</code>.
     *
     * @param card to remove.
     *
     * @return the removed card.
     */
    public boolean getCard(Card card) {
        return hand.remove(card);
    }

    /**
     * Shows the last <code>Card</code> in the list.
     *
     * @return <code>Card</code> object.
     */
    public Card showCard() {
        int card = hand.size() - 1;
        if (card < 0)
            throw new NoSuchCardException("Hand is empty", card);
        return hand.get(card);
    }

    /**
     * Shows the <code>Card</code>.
     *
     * @param card position of the card to show.
     *
     * @return <code>Card</code> object.
     */
    public Card showCard(int card) {
        if (card < 0 || card > hand.size() - 1)
            throw new NoSuchCardException(card);
        return hand.get(card);
    }

    /**
     * Number of elements in the list.
     *
     * @return number of elements in this list.
     */
    public int getNoOfCard() {
        return hand.size();
    }

    /**
     * Removes all cards from the hand.
     */
    public void emptyHand() {
        hand.clear();
    }

    public void sort() {
        Collections.sort(hand);
    }
}